package com.hangwei.paperless.presenter;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.model.bean.MainRepairList;
import com.hangwei.paperless.model.bean.RecordListBean;
import com.hangwei.paperless.net.NetManager;
import com.twelve.all.net.CallBack;
import com.twelve.all.net.ResponseInfo;
import com.twelve.all.presenter.BasePresenter;
import com.twelve.all.utils.SPKey;
import com.twelve.all.utils.SPUtil;
import com.twelve.all.utils.ToastUtil;

import rx.Observable;

/**
 * Created by 张龙臣 on 2020/11/23.
 */

public class MainSeePresenter extends BasePresenter {

    private final SwipeRefreshLayout mRefreshLayout;
    private final BaseQuickAdapter<RecordListBean, BaseViewHolder> mAdapter;
    private final String mUserId;
    private String mBillId;
    private int mPage;
    protected static final int PAGE_SIZE = 10;
    private int mNextRequestPage = 1;

    public void setBillId(String billId) {
        mBillId = billId;
    }

    public void setPage(int page) {
        mPage = page;
    }

    public MainSeePresenter(SwipeRefreshLayout refreshLayout, BaseQuickAdapter<RecordListBean, BaseViewHolder> adapter, RecyclerView recyclerView) {
        this.mRefreshLayout = refreshLayout;
        this.mAdapter = adapter;
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        },recyclerView);
        if (mRefreshLayout != null)
            mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refresh();
                }
            });
        mUserId = SPUtil.getString(SPKey.Account.USER_ID);
    }

    private void refresh() {
        refresh(new CallBack<MainRepairList>() {
            @Override
            public void onCompleted(MainRepairList mainRepairList) {

            }

            @Override
            public void onError() {

            }
        });
    }

    public void refresh(final CallBack<MainRepairList> callBack) {
        mNextRequestPage = 1;
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        NetManager.getInstance().doSubscribe(new NetManager.Model<MainRepairList>(new CallBack<MainRepairList>() {
            @Override
            public void onCompleted(MainRepairList mainRepairList) {
                if (mainRepairList != null) {
                    mAdapter.setEnableLoadMore(true);
                    if (mRefreshLayout != null)
                        mRefreshLayout.setRefreshing(false);
                    setData(true, mainRepairList);
                    if (callBack != null) callBack.onCompleted(mainRepairList);
                }
            }

            @Override
            public void onError() {
                mAdapter.setEnableLoadMore(true);
                if (mRefreshLayout != null)
                    mRefreshLayout.setRefreshing(false);
                if (callBack != null) callBack.onError();
            }
        }) {
            @Override
            protected Observable<ResponseInfo<MainRepairList>> getObservable() {
                return NetManager.getResponseInfoAPI().getRoutingSheetStep(mUserId, mBillId, mPage, 0, mNextRequestPage, PAGE_SIZE);
            }
        });
    }

    private void loadMore() {
        NetManager.getInstance().doSubscribe(new NetManager.Model<MainRepairList>(new CallBack<MainRepairList>() {
            @Override
            public void onCompleted(MainRepairList mainRepairList) {
                setData(false, mainRepairList);
            }

            @Override
            public void onError() {
                mAdapter.loadMoreFail();
            }
        }) {
            @Override
            protected Observable<ResponseInfo<MainRepairList>> getObservable() {
                return NetManager.getResponseInfoAPI().getRoutingSheetStep(mUserId, mBillId, mPage, 0, mNextRequestPage, PAGE_SIZE);
            }
        });
    }

    private void setData(boolean isRefresh, MainRepairList data) {
        mNextRequestPage++;
        final int size = (data == null || data.recordList == null) ? 0 : data.recordList.size();
        if (isRefresh) {
            mAdapter.setNewData(data.recordList);
        } else {
            if (mNextRequestPage == 1) {
                mAdapter.setNewData(data.recordList);
            } else if (size > 0) {
                mAdapter.addData(data.recordList);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局(    不显示且不足一屏就自动加载，page还不对 )
//            mAdapter.loadMoreEnd(!isRefresh);
            mAdapter.loadMoreEnd(false);
            if (mNextRequestPage == 1) {//不需要的提醒
                if (isRefresh) {
                    ToastUtil.showToast("数据更新成功");
                } else
                    ToastUtil.showToast("没有更多数据了");
            }
        } else {
            mAdapter.loadMoreComplete();
        }
    }
}
